package com.zyl.blog.processing.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Redis配置类，用于缓存任务对象
 * @version v1.0
 * @ProjectName: Personal_Blog_Server
 * @Package: com.zyl.blog.tasks.config
 * @ClassName: RedisConfig
 * @Author FinalFantasy
 * @Date 2025/8/12-10:32
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        /* 使用Jackson2JsonRedisSerializer序列化值 */
        GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();

        /* 设置key的序列化方式为String */
        template.setKeySerializer(new StringRedisSerializer());

        /* 设置value的序列化方式为Jackson */
        template.setValueSerializer(serializer);

        /* 设置hash的key和value的序列化方式 */
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();
        return template;
    }
}
